package com.example.tomcatdemo.config;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author ljw
 * @description
 * @date 2021/11/26 8:18
 */
@Configuration
public class CustomConfig {

    /**
     * Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代
     * @param
     * @author ljw
     * @date 2021/11/26 8:21
     */
//    @Bean
//    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
//        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
//            @Override
//            public void customize(ConfigurableWebServerFactory factory) {
//                factory.setPort(8880);
//            }
//        };
//    }
//
//    @Component
//    public static class CustomTomcatServletWebServerFactory extends TomcatServletWebServerFactory{
//        @Override
//        protected void customizeConnector(Connector connector) {
//            super.customizeConnector(connector);
//            connector.setPort(9999);
//        }
//    }

    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        return new TomcatServletWebServerFactory(){
            @Override
            protected void customizeConnector(Connector connector) {
                super.customizeConnector(connector);
                connector.setPort(8989);
            }
        };
    }
}
